{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "165fb431",
   "metadata": {},
   "source": [
    "-------\n",
    ">  2022/09/20/第五周\n",
    "\n",
    "> python笔记\n",
    "-------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c177ba3",
   "metadata": {},
   "source": [
    "# 本周学习内容\n",
    "> 1. 有序的数据结构之列表\n",
    "> 2. 用户输入\n",
    "> 3. 关于格式化文本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "dd419163",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_01_name = \"Marry\"\n",
    "# 思考： 如果想一个变量存放一个班同学的名字，该怎么存？\n",
    "# 思考： 处理数据会不会比较麻烦"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "8187e008",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Marry,周杰伦，吴彦祖\n"
     ]
    }
   ],
   "source": [
    "网一_name_list = \"Marry,周杰伦，吴彦祖\"\n",
    "print(网一_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "89ccf51a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思考2：处理数据会不会比较麻烦？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "28ee645b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'周杰伦，'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list[6:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "0c2eb30f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思考3： 我想知道这个名单有多少个人名？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "0fa68023",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(网一_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "3de5241e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"周杰伦\" in 网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "41686015",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"dage\" in 网一_name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e3397d1",
   "metadata": {},
   "source": [
    "## 1.引入列表的概念\n",
    "* Lists might contain items of different types, but usually the items all have the same type\n",
    "> 注释：我们已经学习了 数值 和 字符，list可以放数值和字符，但是通常要嘛一起放数值，要嘛一起放字符串。\n",
    "\n",
    "> 总结一些通用的有序数据结构的方法：\n",
    ">> 1. 取值：通过索引值取出对应的内容\n",
    ">> 2. 切片：通过索引值区间取出对应的一些内容\n",
    ">> 3. in 可以查看成员对象是否存在\n",
    "### 列表的一些方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "b39f479b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4]\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "57171976",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "a =[1,2,3,4]\n",
    "print(a,type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "c54e14e1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Marry', '周杰伦', '吴彦祖']\n"
     ]
    }
   ],
   "source": [
    "网一_name_list = [\"Marry\",\"周杰伦\",\"吴彦祖\"]\n",
    "print(网一_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "40473920",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Marry', '周杰伦', '吴彦祖'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "网一_name_list = [\"Marry\",\"周杰伦\",\"吴彦祖\"]\n",
    "print(网一_name_list,type(网一_name_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7183e8d9",
   "metadata": {},
   "source": [
    "### 1.1列表的运算法则(Operation)\n",
    "* 列表的数据本身不会修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "9298d807",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'周杰伦'"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "37ede84b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Marry'"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "253b80cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查看有多少位同学"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "19b0314d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(网一_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "448d3ae6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "网一： Marry\n",
      "网一： 周杰伦\n",
      "网一： 吴彦祖\n"
     ]
    }
   ],
   "source": [
    "for i in 网一_name_list:\n",
    "    print(\"网一：\",i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "ce916931",
   "metadata": {},
   "outputs": [],
   "source": [
    "# in可以查询数据是否存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "24131820",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"周杰伦\" in 网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "7f664a22",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"mayun\" in 网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "3e7bcb00",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关于运算法则（in + 切片  取值  len() .count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "5d49e411",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list_转专业 = [\"pengyuyan\",\"chengguanxi\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "78181e8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['pengyuyan', 'chengguanxi', 'Marry', '周杰伦', '吴彦祖']"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list_转专业 + 网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "92a8d7ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 一个list 可不可以出现 重复项"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "f285c009",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_age_list = [18,23,43,18,29,29]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "f6b7d1de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 23, 43, 18, 29, 29]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_age_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aabb76c3",
   "metadata": {},
   "source": [
    "### 2.2列表的方法（Function)  对列表数据进行操作\n",
    "> 1. 增  \n",
    ">> 1. append 增加，增加元素到末尾\n",
    ">> 2. insert 增加元素到某个位置\n",
    "> 2. 删\n",
    ">> 1. remove 删除某个元素元素  如果有重复，删除第一个  如果元素不存在 ，报错\n",
    ">> 2. pop 删除列表尾部的元素，也可以删除某个位置的元素 pop(i) i是指下标\n",
    ">> 3. clear 清除全部元素\n",
    "> 3. 改\n",
    ">> 1. sort 进行排序\n",
    ">> 2. reverse 将列表进行翻转\n",
    ">> 3. extend 合并列表\n",
    ">> 4. copy 浅拷贝对象 不等价与 =\n",
    "> 4. 查\n",
    ">> 1. count 查找某个元素在列表里面的个数\n",
    ">> 2. index  查找元素的位置   如果有重复元素  返回的是元素在列表中的第一个位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "bd77dccd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# append 增加，增加元素到末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "0410bfbc",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list = [\"Marry\",\"周杰伦\",\"吴彦祖\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "a0a5b010",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list.append(\"老六\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "41f051cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '周杰伦', '吴彦祖', '老六']"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "0d0199c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list.append(\"甜蜜老张\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "88083357",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '周杰伦', '吴彦祖', '老六', '甜蜜老张']"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "0997b80c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# insert 增加元素到某个位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "a05bdd88",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网一_name_list.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "2a9292be",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list.insert(1,\"李明\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "99b2af19",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李明', '周杰伦', '吴彦祖', '老六', '甜蜜老张']"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "10d6cc26",
   "metadata": {},
   "outputs": [],
   "source": [
    "# remove 删除某个元素元素  如果有重复，删除第一个  如果元素不存在 ，报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "cfb56895",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网一_name_list.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "9fdfc624",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list.remove(\"周杰伦\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "20bf6c40",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李明', '吴彦祖', '老六', '甜蜜老张']"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "877a519b",
   "metadata": {},
   "outputs": [],
   "source": [
    " # 如果remove的字符不存在 程序就会报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "f644519b",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "list.remove(x): x not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[1;32mIn [63]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43m网一_name_list\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mremove\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m周杰伦\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list"
     ]
    }
   ],
   "source": [
    "网一_name_list.remove(\"周杰伦\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "edfcc47a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# remove 经常和 in 一起使用，先检查成员变量是否存在，然后再remove"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "727356d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李明', '吴彦祖', '老六', '甜蜜老张']"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if \"周杰伦\" in 网一_name_list:\n",
    "    网一_name_list.remove(\"周杰伦\")\n",
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "0e814521",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pop 删除列表尾部的元素，也可以删除某个位置的元素 pop(i) i是指下标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "d8b302a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网一_name_list.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "04c2c66c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'甜蜜老张'"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "2525a767",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李明', '吴彦祖', '老六']"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "90aad907",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'吴彦祖'"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list.pop(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "af4c8b1a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李明', '老六']"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "0561ea79",
   "metadata": {},
   "outputs": [],
   "source": [
    "# sort 进行排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "9b4d470d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function sort:\n",
      "\n",
      "sort(*, key=None, reverse=False) method of builtins.list instance\n",
      "    Sort the list in ascending order and return None.\n",
      "    \n",
      "    The sort is in-place (i.e. the list itself is modified) and stable (i.e. the\n",
      "    order of two equal elements is maintained).\n",
      "    \n",
      "    If a key function is given, apply it once to each list item and sort them,\n",
      "    ascending or descending, according to their function values.\n",
      "    \n",
      "    The reverse flag can be set to sort in descending order.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网一_name_list.sort)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "f46a97fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list = [2,3,4,5,6,1,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "fb192f73",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "61b9af4a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "b34a4a10",
   "metadata": {},
   "outputs": [],
   "source": [
    "# index  查找元素的位置   如果有重复元素  返回的是元素在列表中的第一个位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "46ce86b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function index:\n",
      "\n",
      "index(value, start=0, stop=9223372036854775807, /) method of builtins.list instance\n",
      "    Return first index of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网一_name_list.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "7d98dd2a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list.index(\"老六\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "ab9a7163",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list.index(\"李明\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "179abc60",
   "metadata": {},
   "outputs": [],
   "source": [
    "# reverse 将列表进行翻转"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "0c96f81b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function reverse:\n",
      "\n",
      "reverse() method of builtins.list instance\n",
      "    Reverse *IN PLACE*.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网一_name_list.reverse)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "990c79f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list.reverse()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "af9bebc0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['老六', '李明', 'Marry']"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d2fad14",
   "metadata": {},
   "outputs": [],
   "source": [
    "# clear 清除全部元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "d3073ee9",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list = [\"Marry\",\"李明\",\"甜蜜老张\",\"李明\",\"周杰伦\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "b726e456",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "a3ddb73a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76fb591f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# copy 浅拷贝对象 不等价与 ="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "7f8a9064",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list = [\"Marry\",\"李明\",\"甜蜜老张\",\"李明\",\"周杰伦\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "1e4c6b1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = 网一_name_list.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "5ae5f7e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李明', '甜蜜老张', '李明', '周杰伦']"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "477ef025",
   "metadata": {},
   "outputs": [],
   "source": [
    "# extend 合并列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "d011d4cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function extend:\n",
      "\n",
      "extend(iterable, /) method of builtins.list instance\n",
      "    Extend list by appending elements from the iterable.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网一_name_list.extend)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "e869a669",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list = [\"Marry\",\"李明\",\"甜蜜老张\",\"李明\",\"周杰伦\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "69dd7b8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "网二_name_list = [\"纯路人\",\"枚树枝\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "b90eebe5",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list.extend(网二_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "39adce14",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李明', '甜蜜老张', '李明', '周杰伦', '纯路人', '枚树枝']"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "540782ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "# count 查找某个元素在列表里面的个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "eaed703c",
   "metadata": {},
   "outputs": [],
   "source": [
    "网一_name_list = [\"Marry\",\"李明\",\"甜蜜老张\",\"李明\",\"周杰伦\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "6f206da8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list.count(\"李明\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "cb8fa3e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_name_list.count(\"周杰伦\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "c4c825fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践：请移除19的元素\n",
    "age_list = [18,19,18,19,17,20,20,18,19,16,17,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "916da94e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 18, 19, 17, 20, 20, 18, 19, 16, 17, 20]"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d0e4e98",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思路1：remove(value),先决条件，知道出现的数值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "id": "b9499c29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "删除的数量 = age_list.count(19)\n",
    "for i in range(删除的数量):\n",
    "    if 19 in age_list:\n",
    "        age_list.remove(19)\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "id": "1ef709f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list = [18,19,18,19,17,20,20,18,19,16,17,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "id": "29dd768f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 19\n",
      "2 19\n",
      "6 19\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思路2：pop(index),先决条件，知道值出现的位置\n",
    "for i,v in enumerate(age_list):\n",
    "    if v == 19:\n",
    "        print(i,v)\n",
    "        age_list.pop(i)\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "id": "584da37d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# p68"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "id": "70b8e259",
   "metadata": {},
   "outputs": [],
   "source": [
    "phrase = \"Don't panic!\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "id": "2cd6da49",
   "metadata": {},
   "outputs": [],
   "source": [
    "plist = list(phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "id": "d2e41ff1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n"
     ]
    }
   ],
   "source": [
    "print(phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "id": "c9626322",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    }
   ],
   "source": [
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "id": "9381091f",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(4):\n",
    "    plist.pop()\n",
    "plist.pop(0)\n",
    "plist.remove(\"'\")\n",
    "plist.extend([plist.pop(),plist.pop()])\n",
    "plist.insert(2,plist.pop(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "id": "01fbcf45",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_phrase = ''.join(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "id": "f148d04c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['o', 'n', ' ', 't', 'a', 'p']\n"
     ]
    }
   ],
   "source": [
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "id": "ed2ec3e3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "on tap\n"
     ]
    }
   ],
   "source": [
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "id": "bc3ecdfa",
   "metadata": {},
   "outputs": [],
   "source": [
    "# p81"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "id": "42e7628e",
   "metadata": {},
   "outputs": [],
   "source": [
    "phrase = \"Don't panic!\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "id": "b08406cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "plist = list(phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "id": "4b438300",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n"
     ]
    }
   ],
   "source": [
    "print(phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "id": "e94754f0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    }
   ],
   "source": [
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "id": "7f8ccb73",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_phrase = ''.join(plist[1:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "id": "abca6717",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_phrase = new_phrase + ''.join([plist[5],plist[4],plist[7],plist[6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "id": "cf55352a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    }
   ],
   "source": [
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "id": "87b3e6f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "on tap\n"
     ]
    }
   ],
   "source": [
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a5a2d97",
   "metadata": {},
   "source": [
    "## 列表总结"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c88296f",
   "metadata": {},
   "source": [
    "| 增 | 删 | 改 | 查 |\n",
    "| ---- | ---- | ---- | ---- |\n",
    "| append 增加元素到末尾 | remove 删除某个元素元素 | sort 进行排序 | count 查找某个元素在列表里面的个数 |\n",
    "| insert 增加元素到某个位置 | pop 删除列表尾部的元素 | reverse 将列表进行翻转 | index 查找元素的位置 |\n",
    "|         | clear 清除全部元素 | extend 合并列表 |   |\n",
    "|   |   | copy 浅拷贝对象 |   |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e589fd5a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
